<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * View 通用函数
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-03-05 17:36:33 +0800 (三, 2014-03-05) $
 * @version : $Id: view_helper.php 59 2014-03-05 09:36:33Z spike $
 * @rev     : $Revision: 59 $
 */
// ------------------------------------------------------------------------

/**
 * Doctype
 *
 * 生成标准html声明
 *
 * @param   string  可选值:xhtml-11, xhtml-strict, xhtml-trans, xhtml-frame,html4-strict, html4-trans, and html4-frame.
 * @return  string
 */
if ( ! function_exists('doctype'))
{
    function doctype($type = 'xhtml1-strict')
    {
        global $_doctypes;

        if ( ! is_array($_doctypes))
        {
            if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
            {
                include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
            }
            elseif (is_file(APPPATH.'config/doctypes.php'))
            {
                include(APPPATH.'config/doctypes.php');
            }

            if ( ! is_array($_doctypes))
            {
                return FALSE;
            }
        }

        if (isset($_doctypes[$type]))
        {
            return $_doctypes[$type];
        }
        else
        {
            return FALSE;
        }
    }
}

// ------------------------------------------------------------------------
/**
 * CSS
 * 
 * 生成"css link",CSS_DOMAIN + WEB_THEME + 文件名 + ?v=css版本号
 *
 * @param   array 
 * @return  string
 */

if ( ! function_exists('css'))
{
    function css($data = NULL)
    {
        if( ! isset($data)) 
            return '';

        if( ! is_array($data))
            $data = (Array)$data;

        foreach ($data as $value) 
        {
            if(preg_match('/^(http:\/\/|https:\/\/)/', $value))
            {
                $href = $value."?v=".CSS_VERSION;
            }
            else
            {
                $href = CSS_DOMAIN.$value."?v=".CSS_VERSION;
            }
            $css .= "<link href=\"{$href}\" rel=\"stylesheet\" type=\"text/css\" />";
        }
        return $css;
    }
}

// ------------------------------------------------------------------------
/**
 * JS
 * 
 * 生成"js link",JS_DOMAIN + SYS_THEME + 文件名 + ?v=JS_VERSION
 *
 * @param   array 
 * @return  string
 */

if ( ! function_exists('js'))
{
    function js($data = NULL)
    {
        if( ! isset($data)) 
            return '';
        
        if( ! is_array($data))
            $data = (Array)$data;

        foreach ($data as $value) 
        {
            if(preg_match('/^(http:\/\/|https:\/\/)/', $value))
            {
                $src = $value."?v=".JS_VERSION;
            }
            else
            {
                $src = JS_DOMAIN.$value."?v=".JS_VERSION;
            }
            $js .= "<script src=\"{$src}\" type=\"text/javascript\"></script>";
        }
        return $js;
    }
}

/**
 * 获得带有式样的价格
 * @param  [string]
 * @return [string]
 */
if (!function_exists('price_format')) 
{
    function price_format($price)
    {
        $price = sprintf("%.2f",(float)$price);
        return '<span class="price"><small>￥</small>'.$price.'</span>';
    }
}

/**
 * 格式化时间
 * @param  [string]
 * @return [string]
 */
if (!function_exists('time_format')) 
{
    function time_format($time = '', $type = 'normal')
    {
        switch (strtolower(trim($type))) 
        {
            case 'normal':
            default:
                $pattern = 'Y年m月d日 H:i:s';
                break;
        }


        $timestamp = is_int($time) ? $time : strtotime(trim($time));
        if($timestamp && $timestamp>0)
        {
            return date( $pattern, $timestamp);
        }
        return '';
    }
}


if (!function_exists('goods_img')) 
{
    /**
     * [生成商品图片html]
     * @param  [string]     $goods_sn   [商品编码]
     * @param  [int]        $num        [第几张图片]
     * @param  [enum]       $spec       [图片规格]
     * @param  [string]     $alt        [alt信息]
     * @param  [bool]       $return_src [是否返回路径]
     * @return [string]
     */
    function goods_img($goods_sn, $num, $spec, $alt = "", $return_src = FALSE)
    {
        $num  = (int)$num;
        $spec = strtolower(trim($spec));

        if (   ! preg_match('/^G[0-9]{4}$/i', $goods_sn)
            || $num <= 0
        )
        {
            return ($return_src === TRUE) ? '' : '<img src="" alt="" />';
        }

        switch ($spec) {
            case 'origin':
                break;

            case 'medium':
                    $width  = 150;
                    $height = 150;
                break;

            case 'small':
            default:
                    $width  = 50;
                    $height = 50;
                break;
        }
        $src = FRONTEND_DATA_DOMAIN."goods/{$goods_sn}/{$num}_{$spec}.jpg";
        if($return_src === TRUE)
            return $src;
        
        return "<img class=\"goods-img\" width=\"{$width}\" height=\"{$height}\" src=\"{$src}\" alt=\"{$alt}\" />";
    }
}


/* End of file view_helper.php */